package com.wyc.controller;

import com.wyc.pojo.CensusMessage;
import com.wyc.service.CensusMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CensusMessageController
 * @Author 王韫琛
 * @Date 2021/1/10 1:58
 * @Version 1.0
 */
@Controller
public class CensusMessageController {
    //controller调service层
    @Autowired
    private CensusMessageService censusMessageService;
    //跳转到添加住户信息界面
    @RequestMapping("/houseInformationDis")
    public String houseInformationDis(){
        return "/view/superAdmin_jsp/house_Infor_dis";
    }
    //执行注册住户信息方法
    @ResponseBody
    @RequestMapping("/register")
    public String register(CensusMessage censusMessage){
        censusMessage.setAddtime(Timestamp.from(Instant.now()).toString());
        Map<String, Object> map = new HashMap<>();
        if (censusMessageService.addHouseInfo(censusMessage) >0){
            System.out.println("1");
            map.put("success",true);
            map.put("message","添加成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //检查身份证是否已经存在
    @ResponseBody
    @RequestMapping("/selectByIdentityCard")
    public String selectByIdentityCard(String identity_card){

        Map<String,Object> map = new HashMap<>();
        if (censusMessageService.findCensusMessageIdentityCard(identity_card) != null){
            map.put("success",true);
            map.put("message","身份证已存在,请重新输入");
        } else{
            map.put("success",false);
            map.put("message","可以使用");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //检查身份证是否已经存在
    @ResponseBody
    @RequestMapping("/selectByPhone")
    public String selectByPhone(String phone){
        Map<String,Object> map = new HashMap<>();
        if (censusMessageService.findCensusMessagePhone(phone)!=null){
            map.put("success",true);
            map.put("message","手机号已存在,请重新输入");
        }else {
            map.put("success",false);
            map.put("message","可以使用");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //超级管理员注册完毕后跳转到信息展示界面
    @RequestMapping("/house_Exhibiting_Infor")
    public String house_Exhibiting_Infor(){
        return "/view/superAdmin_jsp/house_Exhibiting_Infor";
    }
    //住户信息展示界面
    @ResponseBody
    @RequestMapping("/findPage")
    public String findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        //调用service得到分页对象
        List<CensusMessage> pageInfo = censusMessageService.findPage(pageNum,pageSize);
        map.put("code",0);
        map.put("msg","");
        map.put("count",censusMessageService.getTotal());//总数量
        map.put("data",pageInfo);//数据列表
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //返回住户信息维护界面
    @RequestMapping("/house_Infor_Maintain")
    public String house_Infor_Maintain(){
        return "/view/superAdmin_jsp/house_Infor_Maintain";
    }
    //根据身份证查找住户
    @RequestMapping("/selectIdentitycard")
    @ResponseBody
    public String selectIdentitycard(String identity_card){
        Map<String,Object> map = new HashMap<>();
        System.out.println(identity_card);
        System.out.println(censusMessageService.selectIdentitycard(identity_card).toString());
        List<CensusMessage> pageInfo =  censusMessageService.selectIdentitycard(identity_card);
        map.put("code",0);
        map.put("msg","");
        map.put("count",1);//总数量
        map.put("data",pageInfo);
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //根据id删除住户信息
    @RequestMapping("/deleteHouseInfoById")
    @ResponseBody
    public String deleteHouseInfoById(Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        if (censusMessageService.deleteHouseInfoById(id)>0){
            map.put("success",true);
            map.put("message","删除成功");
        }else {
            map.put("success",false);
            map.put("message","删除失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);
    }
    //修改用户信息
    @RequestMapping("/updateHouseInfo")
    @ResponseBody
    public String updateHouseInfo(CensusMessage censusMessage){
        System.out.println(censusMessage.toString());
        censusMessage.setAddtime(Timestamp.from(Instant.now()).toString());
        Map<String, Object> map = new HashMap<>();
        if (censusMessageService.updateHouseInfo(censusMessage)>0){
            map.put("success",true);
            map.put("message","修改成功");
        }else {
            map.put("success",false);
            map.put("message","修改失败");
        }
        return cn.hutool.json.JSONUtil.toJsonStr(map);

    }
}
